<script setup>
import { reactive, ref } from 'vue'
import { checkMobile, checkPassword } from '@/utils/validate'
import Upload from '@/components/upload/index.vue'

// 表单数据
const formData = reactive({
  user_id: 0,
  username: '',
  password: '',
  mobile: '',
  state: '1',
  avatar: '',
  role: []
})
// 表单验证
const formRules = reactive({
  username: [
    { required: true, message: '请输入字段名称', trigger: 'blur' },
    { min: 2, max: 6, message: '请输入2~6个字符', trigger: 'blur' }
  ],
  password: [
    { validator: checkPassword, message: '请输入6~12个字符', trigger: 'blur' }
    // { required: true, message: '请输入密码', trigger: 'blur' },
    // { min: 6, max: 12, message: '请输入6~12个字符', trigger: 'blur' }
  ],
  mobile: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { validator: checkMobile, trigger: 'blur' }
  ]
})
// 提交验证
const formRef = ref()
const onSubmit = () => {
  formRef.value.validate((isSuccess) => {
    if (isSuccess) {
      alert('逻辑处理')
    }
  })
}
// const onReset = () => {
//   formRef.value.resetFields()
// }

// 对话框状态
const state = ref(false)

// 暴露
defineExpose({
  state,
  formData
})
</script>
<template>
  <el-dialog
    v-model="state"
    :title="`${formData.mobile ? '修改' : '添加'}字典类型`"
    width="600"
    :before-close="() => (state = false)"
  >
    <!-- 内容 -->
    <el-form
      v-if="state"
      size="large"
      label-width="80"
      :model="formData"
      :rules="formRules"
      ref="formRef"
    >
      <el-form-item label="字段名称" prop="username">
        <el-input placeholder="请输入字段名称" v-model="formData.username" />
      </el-form-item>
      <el-form-item label="字段类型" prop="username">
        <el-input placeholder="请输入字段类型" v-model="formData.username" />
      </el-form-item>

      <el-form-item label="状态" prop="">
        <el-radio-group v-model="formData.state" class="ml-4">
          <el-radio label="1" size="large">正常</el-radio>
          <el-radio label="2" size="large">停用</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="备注">
        <el-input type="textarea" placeholder="请输入备注" :rows="3" />
      </el-form-item>

      <el-form-item class="btns">
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button type="default" @click="state = false">取消</el-button>
      </el-form-item>
    </el-form>
    <!-- 内容 -->
  </el-dialog>
</template>
